package com.swt.gap.service.impl;

import com.swt.gap.dao.EventElementMapper;
import com.swt.gap.dao.OntologyNodePropertyDao;
import com.swt.gap.dao.OntologyRelationshipMapper;
import com.swt.gap.model.entity.EventElement;
import com.swt.gap.model.vo.element.EventElementVO;
import com.swt.gap.service.EventElementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Service
public class EventElementServiceImpl implements EventElementService {

    private final EventElementMapper elementMapper;
    private final OntologyNodePropertyDao ontologyNodePropertyDao;
    private final OntologyRelationshipMapper ontologyRelationshipMapper;

    public EventElementServiceImpl(EventElementMapper elementMapper, OntologyNodePropertyDao ontologyNodePropertyDao, OntologyRelationshipMapper ontologyRelationshipMapper) {
        this.elementMapper = elementMapper;
        this.ontologyNodePropertyDao = ontologyNodePropertyDao;
        this.ontologyRelationshipMapper = ontologyRelationshipMapper;
    }

    @Override
    public Integer addElement(EventElement eventElement) {
        eventElement.setCreatedTime(new Date());
        eventElement.setIsDeleted(false);
        return elementMapper.insertSelective(eventElement);
    }

    @Override
    public Integer deleteElement(Integer elementId) {
        return elementMapper.updateElementBydelete(elementId);
    }

    @Override
    public Integer updateElement(EventElement eventElement) {
        EventElement record = elementMapper.selectByPrimaryKey(eventElement.getElementId());
        record.setElement(eventElement.getElement());
        return elementMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public List<EventElementVO> AllElements(Integer nodeId, Integer createdBy) {
        List<EventElementVO> allElementVOS = new ArrayList<>();
        //查找自定义的要素类型
        List<EventElementVO> customizeElements = elementMapper.selectByEntityAndUser(nodeId, createdBy);

        //事件类型属性和关联节点
        List<EventElementVO> eventElementVOS = new ArrayList<>();

        List<EventElementVO> propertyElements = ontologyNodePropertyDao.selectKeyByOntologyNodeId(nodeId);
        List<EventElementVO> entityElements = ontologyRelationshipMapper.selectEntityByOntologyNodeId(nodeId);
        entityElements.forEach(eventElementVO -> {
            String element = eventElementVO.getElement();
            eventElementVO.setElement(StringUtils.substringAfterLast(element, ","));
        });
        Stream.of(propertyElements, entityElements).forEach(eventElementVOS::addAll);
        Stream.of(customizeElements, eventElementVOS).forEach(allElementVOS::addAll);

        return allElementVOS;
    }

    @Override
    public Boolean judgeElement(Integer nodeId, Integer createdBy, String element, Integer elementId) {
        List<EventElementVO> eventElementVOS = this.AllElements(nodeId, createdBy);
        for (EventElementVO eventElementVO : eventElementVOS) {
            if (eventElementVO.getElement().equals(element)) {
                if (elementId != null) {
                    return !eventElementVO.getElementId().equals(elementId);
                } else {
                    return true;
                }
            }
        }
        return false;
    }


}
